package cn.uexpo.lhz.controller.admin;

import cn.uexpo.lhz.biz.TestBiz;
import cn.uexpo.lhz.common.MqttGateway;
import cn.uexpo.lhz.common.utils.HttpClientUtils;
import cn.uexpo.lhz.domain.TestEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController {

	@Autowired
	private TestBiz testBiz;
	@Autowired
	private MqttGateway mqttGateway;
	 
    @RequestMapping("/sendMqtt.do")
    public String sendMqtt(String sendData,String topic){
        mqttGateway.sendToMqtt(sendData,topic);
        return "OK";
    }



	@RequestMapping(value = "/list", method = { RequestMethod.GET, RequestMethod.POST })
	public List<TestEntity> list() {
		List<TestEntity> list = testBiz.selectByExample(null);
		return list;
	}


	@RequestMapping(value = "/tst", method = { RequestMethod.GET, RequestMethod.POST })
	public String listss(HttpServletResponse response) throws IOException {
    	String filePath="f://gzw.jpg";
		String res =null;
		File newfile =new File(filePath);
		try {
			FileInputStream input = new FileInputStream(newfile);
			byte[] buf = new byte[1024*1024]; //数据中转站 临时缓冲区
			int length = 0;
			//循环读取文件内容，输入流中将最多buf.length个字节的数据读入一个buf数组中,返回类型是读取到的字节数。
			//当文件读取到结尾时返回 -1,循环结束。
			while((length = input.read(buf)) != -1){
				System.out.println(new String(buf, 0, length));
			}
//			text/plain
//			multipart/form-data
			MultipartFile multipartFile =  new MockMultipartFile("file", newfile.getName(), "text/plain", buf);
			//请求图片上传结果
			String key = "b1e899132c9bf7032b/*b86d6f9bc240e4";
			String url1 = "http://sf.uexpo.cn:8080/tool/face5";
//			String url1 = "http://127.0.0.1:8099/client/list";
			String cardType = "2";
			Map<String,String> param = new HashMap<>();
			param.put("key", key);
			param.put("cardType", cardType);
			res = HttpClientUtils.uploadFile(url1, multipartFile, "repayFile", null, null);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}


		return res;

//		response.setHeader("Content-type", "text/html;charset=UTF-8");
//		response.setCharacterEncoding("UTF-8");
//		response.setHeader("Access-Control-Allow-Origin", "*");
//		response.setHeader("Cache-Control","no-cache");
//
//		PrintWriter writer = response.getWriter();
//		writer.write(filePath);
//		writer.close();
//
//		System.out.println(filePath);
	}


}
